package client.comm.baoding.ui

import android.content.Context
import android.graphics.PointF
import android.net.Uri
import android.widget.ImageView
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.api.bean.UserInfo
import client.comm.baoding.databinding.ActivityTabone3Binding
import client.comm.baoding.ui.vm.MainViewModel
import client.comm.commlib.utils.StoreUtil
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target.SIZE_ORIGINAL

import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
import com.davemorrissey.labs.subscaleview.ImageViewState

import com.davemorrissey.labs.subscaleview.ImageSource
import java.io.File


class TabOne3Activity : BaseMiddleActivity<MainViewModel, ActivityTabone3Binding>() {

    val user: UserInfo? by lazy {
        StoreUtil.getBean<UserInfo>(this@TabOne3Activity)
    }

    override fun getLayout(): Int {
        return R.layout.activity_tabone3
    }

    override fun initViewModel(): MainViewModel = createViewModel()

    var type: Int = -1

    override fun initView() {
        intent.getStringExtra("title_name")?.let {
            binding.titleName = it
        }
        type = intent.getIntExtra("type", -1)
        binding.event = this

    }

    private var mageView: SubsamplingScaleImageView? = null

    override fun initData() {
        super.initData()
//        viewModel.viewModelScope.launch {
//            if (type == 0 && user != null && !TextUtils.isEmpty(user!!.zs_img)) {
//                try {
//                    mImgLoader.load(user!!.zs_img).into(binding.ivFenqu)
//                } catch (e: Exception) {
//                }
//            }
//        }
        if (user == null) {
            return
        }
        mageView = findViewById(R.id.iv_fenqu)

        when (type) {
            0 -> { //招商中心
//                var img= "http://49.4.7.100:5000/static/images/20221128/DFZhqTraqEPTWPkHdAAKDS.jpg"

                downloadVisibleImage(user!!.zs_img!!)
            }
            1 -> { //客服中心
                downloadVisibleImage(user!!.kf_img!!)

            }
        }
    }

    //下载图片保存到本地
    private fun downloadVisibleImage(url: String) {
        //普通图片显示方法
//                mImgLoader.load(user!!.zs_img)
//                    .diskCacheStrategy(DiskCacheStrategy.ALL)
//                    .override(SIZE_ORIGINAL, SIZE_ORIGINAL) //关键代码，加载原始大小
//                    .format(DecodeFormat.PREFER_RGB_565) //设置为这种格式去掉透明度通道，可以减少内存占有
//                    .into(binding.ivFenqu2)

//                mageView!!.setQuickScaleEnabled(true)
//                mageView!!.setZoomEnabled(true)
//                mageView!!.setPanEnabled(true)
//                mageView!!.setDoubleTapZoomDuration(100)
//                mageView!!.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP)
//                mageView!!.setDoubleTapZoomDpi(SubsamplingScaleImageView.ZOOM_FOCUS_CENTER)

//                mageView!!.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM)
//                mageView!!.setMinScale(1.0F)

        /* loadPreviewImage(this,"http://49.4.7.100:5000/static/images/20221128/DFZhqTraqEPTWPkHdAAKDS.jpg"
             ,binding.ivFenqu)*/

        //高清长图显示和普通图均可方法
        Glide.with(this).load(url).downloadOnly(object : SimpleTarget<File?>() {

            override fun onResourceReady(resource: File, transition: Transition<in File?>?) {
                mageView!!.setImage(
                    ImageSource.uri(Uri.fromFile(resource)),
                    ImageViewState(0f, PointF(0f, 0f), 0)
                )
            }
        })
    }

    fun loadPreviewImage(context: Context, url: String, target: ImageView) {
        val requestOptions = RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .override(SIZE_ORIGINAL, SIZE_ORIGINAL)//关键代码，加载原始大小
            .format(DecodeFormat.PREFER_RGB_565)//设置为这种格式去掉透明度通道，可以减少内存占有
//            .placeholder(R.drawable.img_error)
//            .error(R.drawable.img_error)
        Glide.with(context)
            .setDefaultRequestOptions(requestOptions)
            .load(url)
            .into(target)
    }

}

